/**
 * 
 */
package org.tinygroup.bizframe.basedao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.tinygroup.application.AbstractApplicationProcessor;
import org.tinygroup.beancontainer.BeanContainerFactory;
import org.tinygroup.config.util.ConfigurationUtil;
import org.tinygroup.logger.LogLevel;
import org.tinygroup.logger.Logger;
import org.tinygroup.logger.LoggerFactory;
import org.tinygroup.xmlparser.node.XmlNode;


/**
 * @author Paul
 *
 */
public class MultipleSequenceApplicationProcessor extends AbstractApplicationProcessor {
	
	private static final String MULTI_SEQ_PATH = "/application/sequence";
	private static final int PROCESSOR_ORDER = 3;
	
	private static final Logger LOGGER = LoggerFactory.getLogger(MultipleSequenceApplicationProcessor.class);
	
	private MultiSequenceProcessor processor =null;
	private XmlNode applicationConfig;
    private XmlNode componentConfig;

	/* (non-Javadoc)
	 * @see org.tinygroup.application.ApplicationProcessor#start()
	 */
	public void start() {
		
		XmlNode sequenceConfig = ConfigurationUtil.combineXmlNode(applicationConfig, componentConfig);
		if(sequenceConfig == null){
			LOGGER.logMessage(LogLevel.WARN, "分布式主键配置缺失，请在application.xml中配置节点{0}",MULTI_SEQ_PATH);
			LOGGER.logMessage(LogLevel.WARN, "<sequence enabled=\"true\"><data-source bean=\"datasource1\" /></sequence>");
			return;
		}
		
		String enabled = sequenceConfig.getAttribute("enabled");
		
		if(!"true".equalsIgnoreCase(enabled)){
			return;
		}
		Map<String,DataSource> mpSources = new HashMap<String,DataSource>();
		List<XmlNode> dataSourceNodes = sequenceConfig.getSubNodes("data-source");
		for(XmlNode node : dataSourceNodes){
			String dataSourceBean = node.getAttribute("bean");
			
			DataSource source =BeanContainerFactory.getBeanContainer(this.getClass().getClassLoader()).getBean(dataSourceBean);
			if(source!=null){
				mpSources.put(dataSourceBean,source);
			}
			else{
				LOGGER.logMessage(LogLevel.WARN, "分布式主键数据源没有配置或者配置不正确{0}",dataSourceBean);
			}
		}
		
		if(!mpSources.isEmpty() && processor !=null){
			processor.init(mpSources);
		}
		
		
	}

	/* (non-Javadoc)
	 * @see org.tinygroup.application.ApplicationProcessor#stop()
	 */
	public void stop() {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see org.tinygroup.config.Configuration#getApplicationNodePath()
	 */
	public String getApplicationNodePath() {
		return MULTI_SEQ_PATH;
	}

	/* (non-Javadoc)
	 * @see org.tinygroup.config.Configuration#getComponentConfigPath()
	 */
	public String getComponentConfigPath() {
		return "/scheduleconfig.config.xml";
	}

	/* (non-Javadoc)
	 * @see org.tinygroup.config.Configuration#config(org.tinygroup.xmlparser.node.XmlNode, org.tinygroup.xmlparser.node.XmlNode)
	 */
	public void config(XmlNode applicationConfig, XmlNode componentConfig) {
		this.applicationConfig = applicationConfig;
        this.componentConfig = componentConfig;
	}

	/* (non-Javadoc)
	 * @see org.tinygroup.config.Configuration#getComponentConfig()
	 */
	public XmlNode getComponentConfig() {
		return componentConfig;
	}

	/* (non-Javadoc)
	 * @see org.tinygroup.config.Configuration#getApplicationConfig()
	 */
	public XmlNode getApplicationConfig() {
		return applicationConfig;
	}

	/* (non-Javadoc)
	 * @see org.tinygroup.commons.order.Ordered#getOrder()
	 */
	public int getOrder() {
		return PROCESSOR_ORDER;
	}

	public MultiSequenceProcessor getProcessor() {
		return processor;
	}

	public void setProcessor(MultiSequenceProcessor processor) {
		this.processor = processor;
	}

}
